package com.xiaoma.aes.encryption.symmetrical;

import com.xiaoma.aes.encryption.asymmetrical.DecodeTool;
import com.xiaoma.aes.encryption.asymmetrical.EncryptTool;
import com.xiaoma.aes.encryption.exception.EncryptionException;
import com.xiaoma.aes.util.BaseUtil;

import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import java.security.Key;
import java.util.Map;


/**
 * 
* @ClassName: AesUtils  
* @Description: AES算法工具类  
* @author baitp  
* @date 2018年12月27日  
*
 */
public final class AESUtil{
    
    /**
     * 256位密匙长度
     */
    public static final Integer AES256 = 256;
    
    /**
     * 生成aes密钥的十六进制
     * @return
     * @throws Exception
     * @version 0.0.1
     * @since 0.0.1
     * @data 2018年11月14日 下午3:24:10
     */
    public static String generateAesKey() throws Exception{
        // 实例化
        KeyGenerator kgen = KeyGenerator.getInstance("AES");
        
        // 设置密钥长度
        kgen.init(256);
        // 生成密钥
        SecretKey skey = kgen.generateKey();
        // 返回密钥的十六进制进制编码
        return BaseUtil.parseByte2HexStr(skey.getEncoded());
    }
    
    
    /**
     * 
    * @Title: encrypt  
    * @Description: 加密
    * @param @param data 内容
    * @param @param key  加密key
    * @param @param pattern 模式
    * @param @return
    * @param @throws EncryptComponentException    参数  
    * @return String    返回类型  
    * @throws
     */
    public static String encrypt(String data, String key){
        String content = "";
        try{
//            content = AESUtil.encrypt(data, key, AES256);
             content = EncryptTool.encryptByPublicKey(data, key);
        }catch(EncryptionException e){
//            LOGGER.error("AES加密、解密处理异常{}", e);
        }
        return content;
    }
    
    
    /**
     * 
    * @Title: decrypt  
    * @Description: 解密
    * @param @param data  内容
    * @param @param key  解密key
    * @param @param pattern 模式
    * @param @return
    * @param @throws EncryptComponentException    参数  
    * @return String    返回类型  
    * @throws
     */
    public static String decrypt(String data, String key){
        String content = "";
        try{
            content = DecodeTool.decryptByPrivateKey(data, key);
        }catch(EncryptionException e){
//            LOGGER.error("AES加密、解密处理异常{}", e);
        }
        return content;
    }
    
    public static void main(String [] args){

     /*   Map<String, Key> stringKeyMap = com.xiaoma.aes.encryption.asymmetrical.KeyGenerator.initKey();
        String publicKey = com.xiaoma.aes.encryption.asymmetrical.KeyGenerator.getPublicKey(stringKeyMap);

        String privateKey = com.xiaoma.aes.encryption.asymmetrical.KeyGenerator.getPrivateKey(stringKeyMap);

        System.out.println("publicKey:"+publicKey);
        System.out.println("privateKey:"+privateKey);
*/


        String data = "mongodb://ats:sd3ER74iasd@172.26.120.57:27017/ats";
        String publicKey = "30819F300D06092A864886F70D010101050003818D0030818902818100E788A6753A1A3FD49EB0B19E9A88421CDB386962C1611207CADA57568302F2E5E4F5316EFB2732D6D7C42DE37287F2AB9F99906EF3BD322E23AFB2F6105E04CB46623AA2D576CE06E7736DC9AF7B649405E469630211A3BC8FFA61F687318B5E5B0B1ADB53697883CD4E4A8DC8AD226F17D0FE0E5C66A0440E0AB51C86311AEB0203010001";
        String privateKey="30820278020100300D06092A864886F70D0101010500048202623082025E02010002818100E788A6753A1A3FD49EB0B19E9A88421CDB386962C1611207CADA57568302F2E5E4F5316EFB2732D6D7C42DE37287F2AB9F99906EF3BD322E23AFB2F6105E04CB46623AA2D576CE06E7736DC9AF7B649405E469630211A3BC8FFA61F687318B5E5B0B1ADB53697883CD4E4A8DC8AD226F17D0FE0E5C66A0440E0AB51C86311AEB020301000102818100C644AD8EAB3FB93360E51C651E1DDAC78D2E84886DE2AB27031DF03684202D5292BB2926C969AACE719C6C535E4B6E926B2C7C9396A3886A1C6323D434F5DE1176F56C7C09ABBFF9E6AAEE2EF5FD8E6DF774B5FAE6BD68B5D4CF7ECC23DEC0E510E23C5D32A30EF115A51C83D261D8825D1BF69027DDBB1E85616D58FEC9A511024100FF61F7EF9859EBB2635E99094A7F0718450588578CE5CA41284DAEC09E72D1733A5609DF12D90F2C0BF825A689EA581F7E6D9DA43F3F4E47AC76883B7812CB89024100E817EC88D569381485F4FA70934637116A1FEA7E58C3D477C0DB3E596D550922B3D10E1637779809F921FC851F243FAF0D43AA831BFDAC54CA917535C5A551D3024100AB1C722A57F30FE8638A3391B5176DA2864491B8CF8FFBB73290F220DF49612B0A90316704D2E8C683728D8F8B62B80ECA001A1ABDC1E5EA4214DD6CB3242CB9024100D97408D70F74BCE965B4E8AC6F201B5A429F529A3C5A868808F6A1CD31C5B9807D0F82EE47DBD2648DBA4D1900DB7A9B6D87178C2DBE9826FD80195C5F290CA7024054FE88B25A1292119E41177756E90B2E6CE7BA1949A179423B956BDB3F60302DB2F5B8221AB2C511AC08CD2A7F3BD9CD3197051C178B4394F8E6738A80A48902";
        String encrypContent="";
        System.out.println(encrypContent=AESUtil.encrypt(data, publicKey));

        System.out.println(AESUtil.decrypt(encrypContent, privateKey));
    }
}
